package com.vmove.childtrain.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;


import com.vmove.childtrain.R;
import com.vmove.childtrain.common.Constants;
import com.vmove.childtrain.db.dao.LightDao;
import com.vmove.childtrain.db.dao.ProgramDao;
import com.vmove.childtrain.db.data.InitData;
import com.vmove.childtrain.db.entity.Light;
import com.vmove.childtrain.utils.SharedPreferences;
import com.vmove.childtrain.view.CountDownView;

import java.util.Calendar;
import java.util.List;

/**
 * 启动页面
 */
public class SplashActivity extends Activity {

    private TextView splash_version_name;
    private TextView splash_copyright;
    private CountDownView countdwonview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        splash_copyright = (TextView) findViewById(R.id.splash_copyright);
        splash_version_name = (TextView) findViewById(R.id.splash_version_name);
        countdwonview = (CountDownView) findViewById(R.id.countdwonview);
        initView();
    }

    private void initView() {
        splash_version_name.setText(getVersionName(this));
//        int backgroundResId = getBackgroundImageResId();
        splash_copyright.setText(getCopyRight(this));
//        splash_image.setImageResource(backgroundResId);

        Animation animation = getBackgroundImageAnimation(this);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        animateBackgroundImage(animation);
    }

    public void animateBackgroundImage(Animation animation) {
        countdwonview.setCountdownTime(4 * 1000);
        countdwonview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (countdwonview.timer != null) {
                    countdwonview.timer.cancel();
                    if (countdwonview.animator != null) {
                        countdwonview.animator.cancel();
//                        splash_image.clearAnimation();
                    }
                }
                readToMain();
            }
        });
        countdwonview.startCountDownTime(new CountDownView.OnCountdownFinishListener() {
            @Override
            public void countdownFinished() {
                //动画结束后的操作
                readToMain();
            }
        });
    }


    public void readToMain() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        hideBottomUIMenu();
    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

    public String getCopyRight(Context context) {
        return context.getResources().getString(R.string.splash_copyright);
    }

    public String getVersionName(Context context) {
        String versionName = null;
        try {
            versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return String.format(context.getResources().getString(R.string.splash_version), versionName);
    }

    /**
     * 加载动画路径
     * @param context
     * @return
     */
    public Animation getBackgroundImageAnimation(Context context) {
        return AnimationUtils.loadAnimation(context, R.anim.splash);
    }
}
